/**
 * @author chaichai
 * @date 2021/12/4/10:34
 */

package com.ui.contact;

import com.controller.ContactController;
import com.controller.GroupController;
import com.pojo.Contact;
import com.pojo.Group;
import com.ui.builder.ButtonBuilder;
import com.ui.builder.ComboboxBuilder;
import com.ui.builder.LabelBuilder;
import com.ui.builder.TextFieldBuilder;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Objects;

public class ModifyContact extends JFrame implements ActionListener {
    private final JPanel inputPanel = new JPanel();
    private final JPanel buttonPanel = new JPanel();
    private final JLabel nameLabel = LabelBuilder.makeLabel("姓名");
    private final JLabel phoneLabel = LabelBuilder.makeLabel("电话");
    private final JLabel genderLabel = LabelBuilder.makeLabel("性别");
    private final JLabel addressLabel = LabelBuilder.makeLabel("住址");
    private final JLabel emailLabel = LabelBuilder.makeLabel("邮箱");
    private final JLabel groupLabel = LabelBuilder.makeLabel("群组");
    private final JLabel starLabel = LabelBuilder.makeLabel("星标");
    private final JLabel[] labels = {nameLabel, phoneLabel, genderLabel, addressLabel, emailLabel, groupLabel, starLabel};
    private final JTextField nameInput = TextFieldBuilder.makeTextField(15);
    private final JTextField phoneInput = TextFieldBuilder.makeTextField(15);
    private final JComboBox<Object> genderChoose = ComboboxBuilder.makeCombobox("", "男", "女");
    private final JTextField addressInput = TextFieldBuilder.makeTextField(15);
    private final JTextField emailInput = TextFieldBuilder.makeTextField(15);
    private final JComboBox<Object> groupChoose = ComboboxBuilder.makeCombobox();
    private final JComboBox<Object> starChoose = ComboboxBuilder.makeCombobox("否", "是");
    private final JComponent[] inputs = {nameInput, phoneInput, genderChoose, addressInput, emailInput, groupChoose, starChoose};
    private final JButton submit = ButtonBuilder.makeButton("确认");
    private final JButton cancel = ButtonBuilder.makeButton("取消");
    private final ContactController contactController = new ContactController();
    private final GroupController groupController = new GroupController();
    private Contact chosenContact;

    public ModifyContact(Contact chosenContact) {
        super("修改联系人");
        if(chosenContact == null) {
            return;
        }
        this.chosenContact = chosenContact;

        //group choose
        List<Group> groupList = groupController.getAllGroups();
        if(groupList == null) {
            JOptionPane.showMessageDialog(null, "获取分组列表失败");
            return;
        }
        groupList.add(0, new Group(""));
        for(Group g: groupList) {
            groupChoose.addItem(g.getValue());
        }

        //fill original information
        nameInput.setText(chosenContact.getName());
        phoneInput.setText(chosenContact.getPhone());
        addressInput.setText(chosenContact.getAddress());
        emailInput.setText(chosenContact.getEmail());
        if(chosenContact.getGender().equals("男")) {
            genderChoose.setSelectedIndex(1);
        }
        else if(chosenContact.getGender().equals("女")) {
            genderChoose.setSelectedIndex(2);
        }
        else genderChoose.setSelectedIndex(0);

        groupChoose.setSelectedIndex(0);
        for(int i = 1; i < groupList.size(); i++) {
            if(chosenContact.getGroup().equals(groupList.get(i).getValue())) {
                groupChoose.setSelectedIndex(i);
                break;
            }
        }

        if(chosenContact.isStar()) starChoose.setSelectedIndex(1);
        else starChoose.setSelectedIndex(0);

        //button listener
        submit.addActionListener(this);
        cancel.addActionListener(this);

        //inputPanel
        inputPanel.setLayout(new GridLayout(8, 2, 10, 10));
        for(int i = 0; i < labels.length; i++) {
            inputPanel.add(labels[i]);
            inputPanel.add(inputs[i]);
        }
        inputPanel.add(new JLabel());
        inputPanel.add(new JLabel());

        //button panel
        buttonPanel.setLayout(new GridLayout(1, 2, 30,0));
        buttonPanel.add(submit);
        buttonPanel.add(cancel);

        //frame
        this.setLayout(new FlowLayout());
        this.setSize(400, 450);
        this.setLocationRelativeTo(null);
        this.add(inputPanel);
        this.add(buttonPanel);
        this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        Object source = e.getSource();
        if(source == submit) {
            Contact newContact = new Contact();
            newContact.setName(nameInput.getText());
            newContact.setPhone(phoneInput.getText());
            newContact.setGender((String) genderChoose.getSelectedItem());
            newContact.setAddress(addressInput.getText());
            newContact.setEmail(emailInput.getText());
            newContact.setGroup((String) groupChoose.getSelectedItem());
            newContact.setStar(Objects.equals(starChoose.getSelectedItem(), "是"));
//            System.out.println(Objects.equals(starChoose.getSelectedItem(), "是"));
            boolean result = contactController.modifyPerson(chosenContact, newContact);
            if(result) {
                JOptionPane.showMessageDialog(null, "修改成功");
            }
            else {
                JOptionPane.showMessageDialog(null, "修改失败");
            }
        }

        this.setVisible(false);
    }
}
